<template>
  <div class="mod-config">
    <table-view ref="view"
                :searchAttrs="searchAttrs"
                :primaryKey="primaryKey"
                :listApiName="listApiName"
                :deleteApiName="deleteApiName"
                :saveAuthName="saveAuthName"
                :deleteAuthName="deleteAuthName"
                :showAttrs="showAttrs"
                :optionWidth="200"
    >
    </table-view>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <productinfo-add-or-update v-if="productInfoVisible" ref="productInfo"></productinfo-add-or-update>
    <client-info-add-or-update v-if="clientInfoVisible" ref="clientInfo"></client-info-add-or-update>
    <productplannotice-add-or-update v-if="productPlanNoticeVisible" ref="productPlanNotice" @refreshDataList="getDataList"></productplannotice-add-or-update>
    <deep-category-add-or-update v-if="deepCategoryInfoVisible" ref="deepCategory" ></deep-category-add-or-update>
    <model-info-add-or-update v-if="modelInfoVisible" ref="modelInfo"></model-info-add-or-update>

  </div>
</template>

<script>
import AddOrUpdate from './orderdetail-add-or-update'
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import ProductinfoAddOrUpdate from '@/views/modules/product/productinfo-add-or-update'
import ProductplannoticeAddOrUpdate from '@/views/modules/product/productplannotice-add-or-update'
import ClientInfoAddOrUpdate from '@/views/modules/client/clientinfo-add-or-update'
import DeepCategoryAddOrUpdate from '@/views/modules/deep/deepcategory-add-or-update'
import {mRequest} from '@/utils/request'
import {exportHandle, isAuth} from '@/utils'
import ModelInfoAddOrUpdate from '@/views/modules/model/modelinfo-add-or-update'

export default {
  mixins: [basicMixins],
  data() {
    return {
      isAuth: isAuth,
      status: 0,
      bigMouth: '',
      boxOrderStatusRadio: 0,
      orderIssuesNumber: 0,
      dialogVisible: false,
      modelInfoVisible: false,
      orderIssuesNumberVisible: false,
      boxOrderDialogVisible: false,
      productPlanNoticeVisible: false,
      productInfoVisible: false,
      clientInfoVisible: false,
      deepCategoryInfoVisible: false,
      bigMouthVisible: false,
      orderPlanNoticeVisible: false,
      productDeviceList: [],
      deviceIdList: [],
      orderDetailList: [],
      productInfoList: [],
      modelInfoList: [],
      tableListData: [],
      deepCategoryList: [],
      deviceId: '',
      onlineTime: '',
      searchAttrs: {
        productId: {
          type: 'autoComplete',
          placeholder: '请输入产品名称',
          querySearchApiName: 'productApi/getProductInfoList'
        },
        productWeight: {
          label: '产品重量',
          placeholder: '请输入重量'
        },
        contractOrderId: {
          type: 'autoComplete',
          placeholder: '请输入合同编号',
          querySearchApiName: 'contractApi/getContractOrderInfoList'
        },
        deviceId: {
          type: 'autoComplete',
          placeholder: '拟上机设备',
          querySearchApiName: 'productApi/getProductDeviceListByParent'
        },
        clientId: {
          label: '客户名称',
          clearable: true,
          type: 'autoComplete',
          placeholder: '请输入客户名称',
          querySearchApiName: 'clientApi/getClientList'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        }
      },
      primaryKey: 'id',
      listApiName: 'orderApi/listOrderDetailByWaitConfirm',
      deleteApiName: 'orderApi/deleteOrderDetail',
      saveAuthName: 'order:orderdetail:save',
      deleteAuthName: 'order:orderdetail:delete',
      showAttrs: {
        orderNo: {
          label: '订单编号',
          width: 140,
          type: 'labelWithStatus',
          statusAttr: 'messageStatus',
          items: [
            {value: 0, text: '未读', tagType: 'danger'}
          ]
        },
        status: {
          label: '状态',
          type: 'radio',
          width: 65,
          items: [
            {value: 2, text: '加急', tagType: 'warning'},
            {value: 3, text: '正常', tagType: 'primary'},
            {value: 90, text: '待确认', tagType: 'info'}
            ]
        },
       // orderTime: {label: '订单时间', type: 'date', width: 100},
        clientName: {label: '客户'},
        productNo: {
          label: '产品编号',
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo'
        },
        orderProductNumberStr: {label: '订单数量'},
        specialMark: {
          label: '箱唛',
          type: 'radio',
          items: [
            {text: '普英唛', value: 0, tagType: 'success'},
            {text: '专用唛', value: 1},
            {text: '普英唛+H', label: 2, tagType: 'warning'}
          ]
        },
        productWeightStr: {label: '订单克数'},
        volumeScope: {label: '订单容量'},
       // boxInfoList: {label: '纸箱', width: 150},
        remark: {label: '备注', width: 100},
        deviceId: {
          label: '上机',
          type: 'radio',
          items: [
            {value: 1, text: '1#-8单', tagType: 'danger'},
            {value: 2, text: '2#-8S',tagType: 'success'},
            {value: 3, text: '3#-8S'},
            {value: 4, text: '4#-9S',tagType: 'success'},
            {value: 5, text: '5#-8S',tagType: 'warning'},
            {value: 99, text: '待分配' ,tagType: 'info'},
            {value: 100, text: '外发' ,tagType: 'info'}
          ]
        },
        deepTypeStr: {label: '深加工'},
        bottleOutDiameter: {label: '瓶口外径'},
        onlineTime: {label: '预计上线时间', type: 'date', width: 95},

        // packRequire: {label: '包装要求'},
        boxPackImage: {label: '箱唛图片', type: 'image'},
        bigMouth: {label:'标记膏霜'},
        deliveryTime: {label: '交货时间', type: 'date', width: 95},
        bottleCapSuit: {
          label: '瓶盖套装',
          type: 'radio',
          items: [
            {value: 0, text: '无', tagType: 'danger'},
            {value: 1, text: '有'}
          ]
        },
        bottleInDiameter: {label: '瓶口内径'},
        headNeckHeight: {label: '头径高度'},
        bottleHeight: {label: '瓶高'},
        customerProductStyle: {
          label: '客户样品',
          type: 'radio',
          items: [
            {value: 0, text: '无', tagType: 'danger'},
            {value: 1, text: '有'}
          ]
        }
      }
    }
  },
  methods: {
    openOrderStatusDialog(order) {
      this.dialogVisible = true
      this.modifyOrder = order
      const {status} = order

      this.status = status
    },
    modifyOrderStatus() {
      if (this.modifyOrder.status === this.status) {
        return
      }
      this.modifyOrder.status = this.status

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.status = 0
        this.dialogVisible = false
        this.getDataList()
      })
    },
    modifyBigMouth() {
      if (this.modifyOrder.bigMouth === this.bigMouth) {
        return
      }
      this.modifyOrder.bigMouth = this.bigMouth

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.bigMouth = ''
        this.bigMouthVisible = false
        this.getDataList()
      })
    },
    openBigMouthDialog(order) {
      this.bigMouthVisible = true
      this.modifyOrder = order
      const {bigMouth} = order

      this.bigMouth = bigMouth
    },

    openBoxOrderStatusDialog(order) {
      this.boxOrderDialogVisible = true
      this.modifyOrder = order
      const {boxOrderStatus} = order

      this.boxOrderStatusRadio = boxOrderStatus
    },
    modifyBoxOrderStatus(){
      if (this.modifyOrder.boxOrderStatus === this.boxOrderStatusRadio) {
        return
      }
      this.modifyOrder.boxOrderStatus = this.boxOrderStatusRadio

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.boxOrderStatusRadio = 0
        this.boxOrderDialogVisible = false
        this.getDataList()
      })
    },
    modifyOrderIssuesNumber() {
      if (this.modifyOrder.orderIssuesNumber === this.orderIssuesNumber) {
        return
      }
      this.modifyOrder.orderIssuesNumber = this.orderIssuesNumber

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.orderIssuesNumber = 0
        this.orderIssuesNumberVisible = false
        this.getDataList()
      })
    },
    openProductPlanNotice(id) {
      this.productPlanNoticeVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productPlanNotice)) {
          this.$refs.productPlanNotice.init(null, {orderDetailId: id}, () => {
            this.$router.push({name: 'product-productplannotice'})
          })
        }
      })
    },
    openProductInfoDialog(cb) {
      this.productInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productInfo)) {
          this.$refs.productInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openClientInfoDialog(cb) {
      this.clientInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.clientInfo)) {
          this.$refs.clientInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openDeepCategoryInfoDialog(cb){
      this.deepCategoryInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.deepCategory)) {
          this.$refs.deepCategory.init(null, {status: 1}, cb)
        }
      })
    },
    openOrderIssuesNumberDialog(order){
      this.orderIssuesNumberVisible = true
      this.modifyOrder = order
      const {orderIssuesNumber} = order

      this.orderIssuesNumber = orderIssuesNumber
    },
    exportPlantFunc(rows){
      this.onlineTime = ''
      this.orderPlanNoticeVisible = true
      this.deviceId = rows[0].deviceId
      this.initProductDeviceListByParent()
      this.initOrderDetailList()
      this.initProductInfoList()
      this.initDeepCategoryList()
      this.initModelInfoList()
      this.tableListData = rows

    },
    initProductDeviceListByParent() {
      mRequest('productApi/getProductDeviceListByParent', {}, (res) => {
        this.productDeviceList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initOrderDetailList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initProductInfoList() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initDeepCategoryList(attrName = '') {
      mRequest('deepApi/deepCategoryList', {}, (res) => {
        this.deepCategoryList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initModelInfoList(attrName = 'modelInfoId') {
      mRequest('modelApi/getModelInfoList', {}, (res) => {
        this.modelInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    updatePlanOrderStatus() {

      const ids =  this.tableListData.map(item => {
        return item[this.primaryKey]
      })

      mRequest('orderApi/updatePlanOrderStatus', {
        'onlineTime': this.onlineTime,
        'deviceId': this.deviceId,
        'tableListData': this.tableListData
      }, (res => {
        let data = res.data
        if (data && data.code == 0) {

          this.$confirm('是否直接导出改计划?', '提示', {
            confirmButtonText: '导出',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            exportHandle('productApi/getProductPlanNoticeExport', {orderDetailIdList: ids})
          }).catch(() => {
          })

          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.orderPlanNoticeVisible = false
              this.getDataList()
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      }))
    }
  },
  components: {
    ProductplannoticeAddOrUpdate,
    ProductinfoAddOrUpdate,
    ModelInfoAddOrUpdate,
    ClientInfoAddOrUpdate,
    DeepCategoryAddOrUpdate,
    AddOrUpdate,
    tableView
  }
}
</script>
